package com.jackstraw.jack_base.datasource.common.entity;

import com.jackstraw.jack_base.datasource.common.factory.DataSourceBuildFactory;
import com.jackstraw.jack_base.util.ObjectUtil;
import com.jackstraw.jack_base.util.exception.BadConfigException;
import com.jackstraw.jack_base.util.exception.enums.ExceptionEnum;
import lombok.extern.slf4j.Slf4j;


/**
 * 数据源切面类
 * <p>
 * 用于对生成提供服务的类进行再次包装
 *
 * @param <T>  真正对外提供服务的类
 * @param <PE> 配置文件类
 */
@Slf4j
public class DataSourceTemplateCut<T extends AbstractTemplate<T, PE>, PE extends AbstractPropertiesEntity<PE>> {

    /**
     * 工厂类
     */
    private DataSourceBuildFactory<T, PE> dataSourceFactory;

    public DataSourceTemplateCut(DataSourceBuildFactory<T, PE> dataSourceFactory) {
        this.dataSourceFactory = dataSourceFactory;
    }

    /**
     * 切面类
     *
     * @param tenantName
     * @param dataSourceName
     * @param propertiesEntity
     * @return
     */
    public T cut(String tenantName, String dataSourceName, PE propertiesEntity) {
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(dataSourceFactory)) {
            log.error("未找到数据源工厂配置");

            throw new BadConfigException(BadConfigException.bulidErrorCode(ExceptionEnum.COMMON_DATASOURCE, "012")
                    , "未找到数据源工厂配置");
        }
        return dataSourceFactory.build(tenantName, dataSourceName, propertiesEntity);
    }

}
